package com.mingmen.canting.caigourenyuan;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.mingmen.canting.R;
import com.mingmen.canting.adapter.CaiGouLeftAdapter;
import com.mingmen.canting.adapter.CaiGouRuKuAdapter;
import com.mingmen.canting.base.BaseBean;
import com.mingmen.canting.chushizhang.ChushiZhangRightFragment;
import com.mingmen.canting.fragment.BaseFragment;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.model.CaiGouListModel;
import com.mingmen.canting.model.EventBean;
import com.mingmen.canting.model.ShopListModel;
import com.mingmen.canting.model.ZhenBanSortDetail;
import com.mingmen.canting.util.GsonUtil;
import com.mingmen.canting.util.MyStringUtils;
import com.mingmen.canting.util.SharePreferenceUtil;
import com.mingmen.canting.zhenbanlibrary.CheckListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Response;


@SuppressLint("ValidFragment")
public class CaiGouZhongfagment extends BaseFragment implements CheckListener, CaiGouLeftAdapter.OnItemClickListener, CompoundButton.OnCheckedChangeListener {
    String mTitle;

    public static CaiGouZhongfagment getInstance(String title) {
        CaiGouZhongfagment sf = new CaiGouZhongfagment();
        sf.mTitle = title;
        return sf;
    }

    private RecyclerView rvSort;
    private CaigouZhongRightFragment mSortDetailFragment;
    private Context mContext;
    private LinearLayoutManager mLinearLayoutManager;
    String getData;
    String token;

    List<String> list = new ArrayList<>();
    CaiGouLeftAdapter caiGouLeftAdapter;
    FragmentTransaction fragmentTransaction;
    Bundle bundle;
    CheckBox checkbox;

    CheckBox checkbox_ziqu;
    List<CaiGouListModel.DataBean> dataBeanList = new ArrayList<>();

    LinearLayout layout_peisong;

    LinearLayout layout_all;

    LinearLayout layout_nodata;
    int pageNumber = 1;
    @Override
    public View initView() {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.fr_chakancaigou, null);
        EventBus.getDefault().register(this);
        TextView card_title_tv = (TextView) v.findViewById(R.id.card_title_tv);
        card_title_tv.setText(mTitle);
        mContext = getActivity();
        checkbox = v.findViewById(R.id.checkbox);
        checkbox_ziqu = v.findViewById(R.id.checkbox_ziqu);
        rvSort = (RecyclerView) v.findViewById(R.id.rv_sort);
        checkbox = v.findViewById(R.id.checkbox);
        layout_all = v.findViewById(R.id.layout_allloft);
        layout_nodata = v.findViewById(R.id.layout_nodata);
        layout_peisong = v.findViewById(R.id.layout_peisong);

        checkbox.setOnCheckedChangeListener(this);
        checkbox_ziqu.setOnCheckedChangeListener(this);

        token = (String) SharePreferenceUtil.get(getActivity(), "token", "");
        if (!list.isEmpty()) {
            list.clear();
        }

        mLinearLayoutManager = new LinearLayoutManager(mContext);
        rvSort.setLayoutManager(mLinearLayoutManager);
        caiGouLeftAdapter = new CaiGouLeftAdapter(getActivity(), dataBeanList);

        rvSort.setAdapter(caiGouLeftAdapter);
        caiGouLeftAdapter.setOnItemClickListener(this);

        return v;
    }


    @Override
    public void initData() {

        bundle = new Bundle();
        fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
        mSortDetailFragment = new CaigouZhongRightFragment();

        HttpResponse.getGongYingOrderBySupplier(token, "0", "", "", "","",1, new StringCallback(getActivity()) {
            @Override
            public void onSuccess(String s, Call call, Response response) {

                BaseBean baseBean = new BaseBean();
                baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                if (baseBean.getCode() == 0) {
                    CaiGouListModel caiGouListModel = new CaiGouListModel();
                    caiGouListModel = GsonUtil.GsonToBean(s, CaiGouListModel.class);
                    if (caiGouListModel.getData().size() == 0) {
                        layout_all.setVisibility(View.GONE);
                        layout_peisong.setVisibility(View.VISIBLE);
                        layout_nodata.setVisibility(View.VISIBLE);
                    } else {
                        layout_all.setVisibility(View.VISIBLE);
                        layout_peisong.setVisibility(View.VISIBLE);
                        layout_nodata.setVisibility(View.GONE);
                    }
                    if (dataBeanList.size() > 0) {
                        dataBeanList.clear();
                    }

                    dataBeanList.addAll(caiGouListModel.getData());
                    for (int i = 0; i < dataBeanList.size(); i++) {
                        list.add(dataBeanList.get(i).getSupplierName());
                    }
                    caiGouLeftAdapter.setDataList(dataBeanList);
                    if (dataBeanList.size() == 0) {
                        bundle.putString("right", "0");
                        fragmentTransaction.add(R.id.lin_fragment, mSortDetailFragment);
                        fragmentTransaction.commit();
                        mSortDetailFragment.setArguments(bundle);
                    } else {
                        bundle.putString("right", MyStringUtils.beanToString(dataBeanList.get(0)));
                        fragmentTransaction.add(R.id.lin_fragment, mSortDetailFragment);
                        fragmentTransaction.commit();
                        mSortDetailFragment.setArguments(bundle);
                    }

                } else if (baseBean.getCode() == 401) {
//                    startActivity(new Intent(WeiHuListActivity.this, LoginActivity.class));
                } else {
                    Toast.makeText(getActivity(), baseBean.getMessage() + "", Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

    public void getData(String token, String st, String startTime, String endTime, String shipping,String commodityId,int pageNumber) {

        HttpResponse.getGongYingOrderBySupplier(token, st, startTime, endTime, shipping,commodityId,pageNumber, new StringCallback(getActivity()) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                CaiGouListModel caiGouListModel = new CaiGouListModel();
                caiGouListModel = GsonUtil.GsonToBean(s, CaiGouListModel.class);
                if (caiGouListModel.getData().size() == 0) {
                    layout_all.setVisibility(View.GONE);
                    layout_peisong.setVisibility(View.VISIBLE);
                    layout_nodata.setVisibility(View.VISIBLE);
                } else {
                    layout_all.setVisibility(View.VISIBLE);
                    layout_peisong.setVisibility(View.VISIBLE);
                    layout_nodata.setVisibility(View.GONE);
                }
                if (dataBeanList.size() > 0) {
                    dataBeanList.clear();
                }

                dataBeanList.addAll(caiGouListModel.getData());
                for (int i = 0; i < dataBeanList.size(); i++) {
                    list.add(dataBeanList.get(i).getSupplierName());
                }
                caiGouLeftAdapter.setDataList(dataBeanList);
                if (dataBeanList.size() > 0) {
                    Intent intent = new Intent("canting.chushizhang.ChushiZhangRightFragment");
                    intent.putExtra("pos", "pos");
                    intent.putExtra("strData", MyStringUtils.beanToString(caiGouListModel.getData().get(0)));
                    caiGouLeftAdapter.setCheckedPosition(0);
                    getActivity().sendBroadcast(intent);
                }

            }
        });
    }


    @Override
    public void onResume() {
        super.onResume();

    }


    private void setChecked(int position, boolean isLeft) {
        Log.d("p-------->", String.valueOf(position));
        caiGouLeftAdapter.setCheckedPosition(position);
    }


    @Override
    public void check(int position, boolean isScroll) {
        setChecked(position, isScroll);
    }


    @Override
    public void onItemClickListener(View view, int pos, CaiGouLeftAdapter.ViewName viewName, List<CaiGouListModel.DataBean> myLiveLists) {
        caiGouLeftAdapter.setCheckedPosition(pos);
        Intent intent = new Intent("canting.chushizhang.ChushiZhangRightFragment");
        intent.putExtra("pos", pos + "");
        intent.putExtra("strData", MyStringUtils.beanToString(myLiveLists.get(pos)));
        getActivity().sendBroadcast(intent);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.checkbox:
                if (isChecked) {
                    getData(token, "0", "", "", "1","",pageNumber);
                } else {
                    getData(token, "0", "", "", "","",pageNumber);
                }

                break;
            case R.id.checkbox_ziqu:
                if (isChecked) {
                    getData(token, "0", "", "", "0","",pageNumber);
                } else {
                    getData(token, "0", "", "", "","",pageNumber);
                }

                break;
        }
    }

    public void onEventMainThread(Integer a) {
        if (a == 2) {
            getData(token, "0", "", "", "","",1);
        }

    }
}